//
//  VipCenteVC.m
//  SelfDriving
//
//  Created by apple on 16/1/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "VipCenteVC.h"
#import "BusinessService+WD.h"
#import "ValidateHelper.h"
@interface VipCenteVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *nexitBtn;
@property (weak, nonatomic) IBOutlet UITableView *viewTable;

@end

@implementation VipCenteVC{
    LoginResponseModel *user;
}
-(void)viewWillAppear:(BOOL)animated{
    [[BusinessService shareInstance]memberCenter:[[NSMutableDictionary alloc]init] success:^(id result) {
        user = result;
        if ([user.ismember intValue] == 0) {
            [_nexitBtn setTitle:@"成为会员" forState:UIControlStateNormal];
        }else{
            [_nexitBtn setTitle:@"续费" forState:UIControlStateNormal];
        }
        [self.viewTable reloadData];
        
    } failure:^(id result) {
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 2;
    }if (section == 1) {
        return 2;
    }if (section == 2) {
        return 1;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==0) {
        return  30;
    }if (section == 1) {
        return 8;
    }
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 160;
        }else if (indexPath.row == 1){
            return 70;
        }
    }else{
        return 50;
    }
    return 0;
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return @"会员特权";
    }
    return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, 160)];
            img.image = [UIImage imageNamed:@"centerbg"];
            [cell.contentView addSubview:img];
            return cell;
        }
        if (indexPath.row == 1) {
            //为了和memberCenter接口请求到的数据和[AppDelegate shareDelegate].user区分开来，将下面临时定义的user改为xhwUser（xhw11月6号改）
            LoginResponseModel *xhwUser = [AppDelegate shareDelegate].user;
//            if ([xhwUser.memberlevel intValue] == 0)
            if ([user.ismember intValue] == 0)
            {
                UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
                UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(10, 35-20, 40, 40)];
                img.image = [UIImage imageNamed:@"grhytx"];
                [cell.contentView addSubview:img];
                
                UILabel *namelable = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 140, 30)];
                namelable.text = @"点亮会员标识";
                namelable.font = [UIFont systemFontOfSize:14];
                [cell.contentView addSubview:namelable];
                
                UILabel *time = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 200, 30)];
                time.text = @"可享受7项特权";
                time.font = [UIFont systemFontOfSize:14];
                [cell.contentView addSubview:time];
                time.textColor = colorGrey;
                
                
                UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(SCRE_WIDTH-100,20 , 80, 30)];
                lable.backgroundColor = UIColorFromRGB(0x68DA64);
                lable.text = @"成为会员";
                lable.textAlignment = NSTextAlignmentCenter;
                lable.textColor = [UIColor whiteColor];
                lable.font = [UIFont systemFontOfSize:14];
                lable.layer.cornerRadius = 5;
                lable.clipsToBounds = YES;
                [cell.contentView addSubview:lable];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                return cell;
            }else{
                UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
                UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(10, 35-20, 40, 40)];
                img.image = [UIImage imageNamed:@"grhytxs"];
                [cell.contentView addSubview:img];
                
                UILabel *namelable = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 140, 30)];
//                namelable.text = user.nickname;
                namelable.text = user.member[@"nickname"];
                namelable.font = [UIFont systemFontOfSize:14];
                [cell.contentView addSubview:namelable];
                
                UILabel *time = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 200, 30)];
//                time.text = @"到期时间：2015-09-09";

                NSString* doubleTimeStr = user.member[@"time"];
                double doubleTime = doubleTimeStr.doubleValue;
                NSString *updateTime = [ValidateHelper doubleConvertDate:doubleTime FormatterStr:@"yyyy-MM-dd"];
                time.text = [NSString stringWithFormat:@"到期时间:%@",updateTime];

                time.font = [UIFont systemFontOfSize:14];
                [cell.contentView addSubview:time];
                time.textColor = colorGrey;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                return cell;
            }
        }
    }
    if (indexPath.section == 1) {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell3"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"grhymy"];
            cell.textLabel.text = @"会员漫游";
        }
        if (indexPath.row == 1) {
            cell.imageView.image = [UIImage imageNamed:@"grhhdx"];
            cell.textLabel.text = @"定向隐身";
        }
        return cell;
    }
    if(indexPath.section == 2 ){
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell4"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = @"会员功能介绍";
        return cell;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            if([user.ismember intValue]==0){//不是会员
                UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                             bundle:[NSBundle mainBundle]];
                UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"PurchasingMemberVC"];
                [self.navigationController pushViewController:searchResultDetailController animated:YES];
            }else{
            }
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            if([user.ismember intValue]==0){//不是会员
                UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"FJ"
                                                                             bundle:[NSBundle mainBundle]];
                WebViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"WebVC"];
                searchResultDetailController.titlename = @"会员漫游";
                searchResultDetailController.Url = user.roamurl;
                [self.navigationController pushViewController:searchResultDetailController animated:YES];
                
            }else{
                UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                             bundle:[NSBundle mainBundle]];
                UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"MemberRoamingVC"];
                [self.navigationController pushViewController:searchResultDetailController animated:YES];
            
            }

        }
        if (indexPath.row == 1) {
            if([user.ismember intValue]==0){//不是会员
                UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"FJ"
                                                                             bundle:[NSBundle mainBundle]];
                WebViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"WebVC"];
                searchResultDetailController.titlename = @"定向隐身";
                searchResultDetailController.Url = user.descurl;
                [self.navigationController pushViewController:searchResultDetailController animated:YES];
                
            }else{
                UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                             bundle:[NSBundle mainBundle]];
                UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DirectionalStealthVC"];
                [self.navigationController pushViewController:searchResultDetailController animated:YES];
            }
        }
    }
    if(indexPath.section == 2){
        if (indexPath.row==0) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"FJ"
                                                                         bundle:[NSBundle mainBundle]];
            WebViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"WebVC"];
            searchResultDetailController.titlename = @"会员功能介绍";
            searchResultDetailController.Url = user.descurl;
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
            
        }
    }
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
